package com.example.homework.h2021_04_28.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.homework.R;
import com.example.homework.h2021_04_28.fragment.adapter.CircleAdapter;
import com.example.homework.h2021_04_28.fragment.bean.CircleBean;
import com.example.homework.h2021_04_28.network.RetrofitFactory;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CircleFragment extends Fragment {

    private SmartRefreshLayout smartRefreshLayout;
    private RecyclerView rvDynamic;


    //列表的适配器
    private CircleAdapter circleAdapter;
    //每次请求的页数
    private int page =1;

    //每一次请求的数据条数
    private int pageSize =15;
    //创建显示的视图
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_circle,container,false);
    }

    //视图初始化完成后，赋初值
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        smartRefreshLayout = view.findViewById(R.id.refresh_layout);
        rvDynamic = view.findViewById(R.id.rv_dynamic);
        rvDynamic.setLayoutManager(new LinearLayoutManager(getContext(),RecyclerView.VERTICAL,false));
        circleAdapter = new CircleAdapter(getContext());
        rvDynamic.setAdapter(circleAdapter);
        //监听上拉加载下拉刷新的动作
        smartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            //上拉加载更多
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getCircles();
            }

            //下拉刷新
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page=1;
                getCircles();
            }
        });

        getCircles();
    }

    //动态获取数据
    private void getCircles() {
        new Thread(){
            @Override
            public void run() {
                super.run();
                RetrofitFactory.getInstance().API().getCircleInfos(page,pageSize).enqueue(new Callback<CircleBean>() {
                    @Override
                    public void onResponse(Call<CircleBean> call, Response<CircleBean> response) {
                        //停止加载更多
                        smartRefreshLayout.finishLoadMore();
                        //停止刷新
                        smartRefreshLayout.finishRefresh();
                        //如果请求的数据端返回的是正确的状态
                        if (response.isSuccessful()){
                            List<CircleBean.DataBean.RowsBean> beans = response.body().getData().getRows();
                            if (page==1) {
                                circleAdapter.setBeans(beans, false);
                            }else {
                                circleAdapter.setBeans(beans,true);
                            }
                            if (beans.size()<pageSize){
                                smartRefreshLayout.setEnableLoadMore(false);
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<CircleBean> call, Throwable t) {

                    }
                });
            }
        }.start();
    }
}
